var SITE="http://"+document.domain+"/";
var IMAGES_SITE=SITE;
var iframedir="/";
var ROOT="/themes/xyo2o/wap/";
var INDEXPAGE="/index.php";
var inapp=false; 
var CLICK="click";
var siteid,ymdian,koudai,shopid;
var rd=Date.parse(new Date())/1000;//随机数
var oc_ssid,authpara='';
var basejs=["js/jquery.flexslider-min.js"];
/**JS加载器**/
function addjs(i,jss,loadjs_init){
	var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement,
	script,options,s;
	var i=i;
	s = options || {};
	var url = ROOT+jss[i]+"?"+rd;
	script = document.createElement("script");
	script.async = false;
	script.type = "text/javascript";
	script.charset="utf-8";
	script.src = url;
	head.insertBefore(script, head.firstChild);
	script.addEventListener("load", function() {
		if(i<jss.length-1){
			addjs(i+1,jss,loadjs_init);
		}else{
					 
			loadjs_init();
		}
	},false);
	 
	
}
function loadjs(jss,loadjs_init){
	setTimeout(function(){ 
	addjs(0,jss,loadjs_init);
	},100);
}
function gourl(url){
	if(inapp){
		var wvs=plus.webview.all();
		var wvs_n=wvs.length;
		if(wvs_n>5){
			wvs[1].close();
			wvs[2].close();
		}
		var w=plus.webview.create(SITE+url,url,{zindex:1000});
		
		w.addEventListener("loading",function(){
			
			plus.nativeUI.showWaiting();
		})
		 
		w.addEventListener( "loaded", function(){
				w.show("slide-in-right");
				plus.nativeUI.closeWaiting();
		}, false );
	}else{
		window.location=url;
	}
	
}

function goBack(goindex,wbid){
	if(typeof(goindex)=="undefined") goindex="-1";
	if(inapp){
		if(goindex==-2){
			var wobj = plus.webview.currentWebview();
			var opener = wobj.opener();
			opener.hide();
			wobj.hide("slide-out-right"); 
		}else{
			var wobj = plus.webview.currentWebview();
			wobj.hide("slide-out-right"); 
		}
	}else{
		if (document.referrer != null && document.referrer != "") {        
	         window.history.go(goindex);
	     } else{
	     		window.history.go(goindex);
	     		setTimeout(function(){
	     			window.location=INDEXPAGE;
	     		},300)
				
		}
	}
	
}


function get(url,data,fun){
	if(url.indexOf("?")<0){
		url=url+"?ajax=1&CORS=1"+authpara+"&rd="+Date.parse(new Date())/1000
	}else{
		url=url+"&ajax=1&CORS=1"+authpara+"&rd="+Date.parse(new Date())/1000
	}
	if(oc_ssid){
		url=url+"&oc_ssid="+oc_ssid;
	}
	 
	var fun=fun;
	if(fun==undefined){
		fun=data;
		$.get(url,function(data){
			fun(data);
		},"json");
	}else{
		$.get(url,data,function(data){
			fun(data);
		},"json");
	}
	 
	
	 
}

function post(url,data,fun){
	if(url.indexOf("?")<0){
		url+="?ajax=1&CORS=1"+authpara+"&rd="+Date.parse(new Date())/1000;
	}else{
		url+="&ajax=1&CORS=1"+authpara+"&rd="+Date.parse(new Date())/1000;
	}
	if(oc_ssid){
		url=url+"&oc_ssid="+oc_ssid;
	}
	var xhr=null;
	var fun=fun;
	if(fun==undefined){
		fun=data;
		$.post(url,function(data){
			fun(data);
		},"json");
	}else{
		$.post(url,data,function(data){
			fun(data);
		},"json");
	}
	
}


function err_user_head(obj,t){
	//event.preventDefault();
	if(t==1){
		obj.src="/static/images/user_head.jpg.small.jpg";
	}else{
		obj.src="/static/images/user_head.jpg.100x100.jpg";
	}
	
}

function err_imgurl(obj,t){
	//event.preventDefault();
	if(t==1){
		obj.src="/static/images/no_image.jpg.small.jpg";
	}else if(t==2){
		obj.src="/static/images/no_image.jpg.middle.jpg";
	}else{
		obj.src="/static/images/no_image.jpg.100x100.jpg";
	}
}

/*弹框*/
function showbox(title,content,width,height){
	if($("#showbox_container").length==0){
		var html='<div id="showBox_opac" ></div><div id="showBox" onDblClick="showboxClose()" style=" width:100%; height:100%;  "><div id="showbox_container"><div id="showBox_nav"><div id="showBox_title">  </div><div id="showBox_close" onclick="showboxClose()">关闭</a></div></div><div id="showBox_content"></div><div id="showBox_footer"></div></div></div>'; 
		$("body").append(html); 
	}
	$("#showBox_title").html(title);
	$("#showBox_content").html(content);
	$("#showBox").show();
	$("#showBox_opac").show();
	var mt= Math.max(10,( window.innerHeight-parseInt(height))/2);
	$("#showbox_container").css({width:width-10,minHeight:height,marginTop:mt});
	setTimeout(function(){
		width=$("#showbox_container").css("width");
		height=$("#showbox_container").css("height");
		height=parseInt(height);
		width=parseInt(width);
		var mt= Math.max(10,( window.innerHeight-parseInt(height))/2);
		$("#showbox_container").css({width:width-10,minHeight:height,marginTop:mt});
	},300);
	 
}

function showboxClose(){
	$("#showBox_opac").hide();
	$("#showBox_content").html("");
	$("#showBox").css("display","none");
}
function botnav_cart_num(){
	$.get("/index.php?m=ajax&a=botnav_cart_num",function(data){
		if(data>0){
			$("#botnav_carnum").show().html(data);
		}else{
			$("#botnav_carnum").hide();
		}
		setTimeout(function(){
			botnav_cart_num();
		},30000);
	});
	
}
/*Start toast*/
function skyToast(message,type){
	if(typeof(type)=="undefined"){
		type="";
	}
	if($("#toast").length==0){
		var html="<div id='toast'><div class='bg "+type+"'>"+message+"</div></div>";
		$("body").append(html);		
	}else{
		$("#toast").html("<div class='bg "+type+"'>"+message+"</div>");
	}
	$("#toast").show();
	setTimeout(function(){
		$("#toast").hide();
	},2000);
}
/*End toast*/
/*图片缩略*/
function imgResize(w,h){
	 //var w=240,h=160;
	var iw=$(".imgResize:eq(0)").width();
	var ih=h*iw/(2*w);
	$(".imgResize").css({height:ih,overflow:"hidden"});
}

function imgLazy(o){
	if(typeof(o)=='undefined') {
		var o='body';
	}
	setTimeout(function(){
		var obj=$(o).find(".lazy");
		var len=obj.length;
		for(var i=0;i<len;i++){
			var a=obj.eq(i);
			if(a.attr("url")!=a.attr("src")){
				a.attr("src",a.attr("url"));
			}
			
		}
	},200);
}

setTimeout(function(){
		return false;
		if(window.plus){
		 	inapp=true;
			$('body').on("click",".goBack",function(e){
				e.preventDefault();
				goBack(); 
				
			});
			
			$('body').on('click', 'a', function(e) {
					
					var id = this.getAttribute('href');
					 
					
				 
					if(id.indexOf("javascript")>=0 || id.indexOf("JavaScript")>=0  || id.indexOf("tel:")>=0 || id.indexOf("#")>=0 || $(this).hasClass("goBack" || !id || id==undefined)) {
						
					}else{
						e.preventDefault();
						gourl(id);
					}
					
			});
			
			
		}
},600);


/****************End Base*******************************/
$(function(){
	$(document).on("click",".delete",function(){
		var url=$(this).attr("url");
		var obj=$(this);
		if(confirm("删除后不可恢复，确定删除吗?")){
			$.get(url,function(data){
				if(data.error==1){
					alert(data.message);
				}else{
					obj.parents("tr").remove();
				}
			},"json");
			
		}
	});
	
	$(document).on("click",".fav_shop_add",function(){
		var obj=$(this);
		$.get("/index.php?m=fav&a=add&tablename=shop&object_id="+$(this).attr("v"),function(data){
			if(data.error==0){
				obj.attr("class","fav_shop_cancel");
			}
			skyToast(data.message);
		},"json");
	});
	
	$(document).on("click",".fav_shop_cancel",function(){
		var obj=$(this);
		$.get("/index.php?m=fav&a=delete&tablename=shop&object_id="+$(this).attr("v"),function(data){
			if(data.error==0){
				obj.attr("class","fav_shop_add");
			}
			
			skyToast(data.message);
		},"json");
	});
	
	
	
});
 